import logging
from logging.handlers import TimedRotatingFileHandler

from flask.logging import default_handler


def set_logger(app):
    """
    设置 Flask app 的logger
    """
    app.logger.removeHandler(default_handler)
    hdr = logging.StreamHandler()
    if app.config.get('DEBUG'):
        hdr.setLevel(logging.DEBUG)
        app.logger.setLevel('DEBUG')
    else:
        hdr.setLevel(logging.INFO)
        app.logger.setLevel('INFO')

    LOG_FORMAT = "[pid: %(process)d] [%(asctime)s] [%(levelname)s] [%(filename)s:%(lineno)d %(funcName)s] - %(message)s"
    hdr.setFormatter(logging.Formatter(LOG_FORMAT))
    # hdr.setFormatter(formatter)

    for log in (app.logger, ):
        if log:
            log.addHandler(hdr)
